<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class user_uqcms extends control
{
    function __construct()
    {
        $this->uid = @$_SESSION['user']['uid'];
        parent::__construct();
    }
    public function index()
    {
        if ($_POST) {
            $data['name'] = $_POST['name'] ? $_POST['name'] : '';
            $data['nick'] = $_POST['nick'] ? $_POST['nick'] : '';
            $data['sex'] = isset($_POST['sex']) ? $_POST['sex'] : '0';
            $data['address'] = trim($_POST['address']);
            $query = $this->db->update(table('member'), $data, "uid = " . $this->uid);
            if ($query) {
                showmsg('修改成功');
            } else {
                showmsg('修改失败');
            }
        }
        $this->assign('row', $this->module('user')->get_one($this->uid));
        $this->display();
    }
    function email()
    {
        if ($_POST) {
            $email = isset($_POST['email']) ? $_POST['email'] : showmsg('地址不能为空');
            $row = $this->db->get_one("select uid,email from " . table('member') . " where email = '" . $email . "' limit 1");
            if (!$row['uid']) {
                $query = $this->db->update(table('member'), array('email' => $email), 'uid = ' . $this->uid);
                if ($query) {
                    showmsg('修改成功');
                } else {
                    showmsg('修改失败');
                }
            } else {
                showmsg('该邮箱已被占用');
            }
        } else {
            $this->display();
        }
    }
    function mobile()
    {
        $this->assign('row', $this->module('user')->get_one($this->uid));
        $this->display();
    }
    function mobile_cknext()
    {
        $sms_code = isset($_POST['sms_code']) ? $_POST['sms_code'] : exit('error');
        if ($sms_code == strtolower($_SESSION['sms_code'])) {
            $row = $this->db->get_one("select uid,mobile from " . table('member') . " where uid = " . $this->uid . " limit 1");
            if ($row['uid']) {
                $ckcode = rand_number('10');
                $_SESSION['sms_ck_number'] = $ckcode;
                exit(json_encode(array('error' => '0', 'code' => $ckcode)));
            } else {
                exit(json_encode(array('error' => '1', 'msg' => '提交错误')));
            }
        } else {
            exit(json_encode(array('error' => '1', 'msg' => '短信验证码错误')));
        }
    }
    function mobile_update()
    {
        $ckCode = $_GET['ck'];
        if ($_SESSION['sms_ck_number'] != $ckCode && $_SESSION['sms_ck_number'] != '') {
            header("Location:?m=user&a=mobile");
        }
        $this->display();
    }
    function mobile_update_act()
    {
        $mobile = $_POST['mobile'];
        $sms_code = $_POST['sms_code'];
        if ($sms_code == strtolower($_SESSION['sms_code'])) {
            if ($mobile === $_SESSION['sms_mobile']) {
                $row = $this->db->get_one("select uid,mobile from " . table('member') . " where mobile = '" . $mobile . "'");
                if (!$row['uid']) {
                    $query = $this->db->update(table('member'), array('mobile' => $mobile), 'uid = ' . $this->uid);
                    if ($query) {
                        $_SESSION['sms_ck_number'] = '';
                        $_SESSION['user']['mobile'] = hideMobile($mobile);
                        exit('1');
                    } else {
                        exit('修改错误');
                    }
                } else {
                    exit('该电话已经存在');
                }
            } else {
                exit('手机号码和验证的不一致');
            }
        } else {
            exit('短信验证码错误');
        }
    }
    public function password()
    {
        if ($_POST) {
            $oldpasswd = trim($_POST['oldpasswd']) ? $_POST['oldpasswd'] : showmsg('旧密码不能为空');
            $newpasswd = trim($_POST['newpasswd']) ? $_POST['newpasswd'] : showmsg('新密码不能为空');
            $reppasswd = trim($_POST['reppasswd']) ? $_POST['reppasswd'] : showmsg('重复密码不能为空');
            $row = $this->db->get_one("select * from " . table('member') . " where uid = " . $this->uid);
            if ($row['password'] == uqcms_password_hash($oldpasswd, $row['rand'])) {
                if ($newpasswd == $reppasswd) {
                    $row = $this->db->get_one("select * from " . table('member') . " where uid = " . $this->uid);
                    $setsql['password'] = uqcms_password_hash($newpasswd, $row['rand']);
                    $query = $this->db->update(table('member'), $setsql, ' uid = ' . $this->uid);
                    if ($query) {
                        showmsg('保存成功');
                    }
                } else {
                    showmsg('两次密码不正确');
                }
            } else {
                showmsg('旧密码不正确');
            }
        }
        $this->display();
    }
    public function connect_cancel()
    {
        $conn = isset($_GET['conn']) ? $_GET['conn'] : showmsg('CONN IS ERROR');
        $typename = $conn . '_uid';
        $query = $this->db->update(table('member'), array($typename => ''), 'uid = ' . $this->uid);
        if ($query) {
            showmsg('取消成功');
        } else {
            showmsg('取消失败');
        }
    }
    public function upload()
    {
        $path = isset($_GET['path']) ? $_GET['path'] : 'goods';
        $cid = isset($_GET['cid']) ? $_GET['cid'] : '0';
        $result = $this->module('album')->upload($path, $cid);
        echo json_encode($result);
    }
    public function reg_shop()
    {
        $type = isset($_GET['type']) ? $_GET['type'] : '';
        if ($type == '') {
            echo '<script>location.href="member.php?m=user&a=reg_shop&type=1"</script>';
        }
        if (cfg('shop_apply') == '1') {
            $row = $this->db->get_one("select * from " . table('shop_auth') . " where uid = " . $this->uid . " limit 1");
            if ($row && $row['id']) {
                if ($row['status'] == '1') {
                    $sprow = $this->db->get_one("select * from " . table('shop') . " where uid = " . $this->uid . " limit 1");
                    if ($sprow['id'] && $sprow['status'] == '1' && $row['status'] == '1') {
                        $this->module('user')->create_shop_session($sprow);
                        echo '<script>location.href="member.php?m=shop"</script>';
                    }
                } else {
                    $row['license_start_time'] = date('Y-m-d', $row['license_start_time']);
                    $row['license_end_time'] = date('Y-m-d', $row['license_end_time']);
                    $this->assign('row', $row);
                }
            }
        }
        $this->display();
    }
    public function reg_shop_save()
    {
        if ($_POST) {
            $authsql['uid'] = $this->uid;
            $authsql['id_name'] = isset($_POST['id_name']) ? $_POST['id_name'] : error_json('证件姓名不能为空');
            $authsql['id_type'] = isset($_POST['id_type']) ? $_POST['id_type'] : error_json('证件类型不能为空');
            $authsql['id_code'] = isset($_POST['id_code']) ? $_POST['id_code'] : error_json('证件号码不能为空');
            $authsql['id_img_up'] = isset($_POST['id_img_up']) ? $_POST['id_img_up'] : error_json('正面照不能为空');
            $authsql['id_img_down'] = isset($_POST['id_img_down']) ? $_POST['id_img_down'] : error_json('反面照不能为空');
            $authsql['holder_img'] = isset($_POST['holder_img']) ? $_POST['holder_img'] : error_json('手持证件照不能为空');
            $type = intval($_POST['type']) ? $_POST['type'] : '1';
            if ($type == '1') {
                $authsql['shop_type'] = '1';
            } elseif ($type == '2') {
                $authsql['shop_type'] = '2';
                $authsql['license_img'] = isset($_POST['license_img']) ? $_POST['license_img'] : error_json('营业执照不能为空');
                $authsql['license_code'] = isset($_POST['license_code']) ? $_POST['license_code'] : error_json('营业执照不能为空');
                $authsql['license_start_time'] = isset($_POST['license_start_time']) ? strtotime($_POST['license_start_time']) : error_json('营业执照开始时间不能为空');
                $long_time = isset($_POST['license_long_time']) ? strtotime($_POST['license_long_time']) : '0';
                if ($long_time == '1') {
                    $authsql['license_end_time'] = isset($_POST['license_end_time']) ? strtotime($_POST['license_end_time']) : error_json('营业执照结束时间不能为空');
                    $authsql['license_long_time'] = '0';
                } else {
                    $authsql['license_end_time'] = '0';
                    $authsql['license_long_time'] = '1';
                }
            }
            $authsql['apply_time'] = time();
            $authsql['status'] = '0';
            $authsql['addtime'] = time();
            $row = $this->db->get_one("select * from " . table('shop_auth') . " where uid = " . $this->uid);
            if ($row['id']) {
                $query = $this->db->update(table('shop_auth'), $authsql, 'uid = ' . $this->uid);
                if ($query) {
                    $result['error'] = '0';
                    $result['msg'] = '申请成功';
                    echo json_encode($result);
                } else {
                    error_json('申请失败');
                }
            } else {
                $query = $this->db->add(table('shop_auth'), $authsql);
                if ($query) {
                    $result['error'] = '0';
                    $result['msg'] = '申请成功';
                    echo json_encode($result);
                } else {
                    error_json('申请失败');
                }
            }
        } else {
            error_json('提交错误');
        }
    }
    function entersp()
    {
        $row = $this->db->get_one("select * from " . table('shop') . " where uid = " . $this->uid);
        if ($row['id']) {
            $this->module('user')->create_shop_session($row);
            header("Location:member.php?m=shop");
        } else {
            header("Location:member.php?m=user&a=reg_shop");
        }
    }
}